package com.ziiwee.reduce.describe.creators.controllers;

import com.ziiwee.reduce.describe.HandlerDescribe;
import com.ziiwee.reduce.describe.creators.handlers.ControllerHandlerDescribeCreator;
import org.springframework.core.ResolvableType;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;

import java.lang.reflect.AnnotatedElement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ziiwee
 * @data 2020/8/12
 */
public class DefaultControllerDescribeCreator extends AbstractControllerDescribeCreator {

    protected final Class<?> beanType;
    protected final Map<RequestMappingInfo, HandlerMethod> mappings;

    public DefaultControllerDescribeCreator(Class<?> beanType, Map<RequestMappingInfo, HandlerMethod> mappings) {
        this.beanType = beanType;
        this.mappings = mappings;
    }



    @Override
    public AnnotatedElement getElement() {
        return beanType;
    }

    @Override
    public ResolvableType getDefaultType() {
        return ResolvableType.forClass(beanType);
    }

    @Override
    public List<HandlerDescribe> getHandlers() {
        List<HandlerDescribe> handlers = new ArrayList<>();
        mappings.forEach((k, v) -> {
            handlers.add(new ControllerHandlerDescribeCreator(k, v).create());
        });
        return handlers;
    }

    @Override
    public ResolvableType getSubjectType() {
        return null;
    }
}
